package com.jwsoft.manager.core.util;

import cn.hutool.core.convert.Convert;
import com.jwsoft.manager.common.vo.eduAddressTree.AddressTreeVO;
import com.jwsoft.manager.common.vo.eduAddressTree.AddressVO;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 地址信息静态工具类
 *
 * @author fangs
 */
public class AddressUtil {

    private static final List<String> AREA_TYPE_SORT = Arrays.asList("province", "city", "district", "town", "village",
            "road", "roadNumber", "detail", "houseNumber", "unitNumber", "roomNumber");

    private static final List<String> AREA_ID_SORT = Arrays.asList("detailId", "roadId", "villageId", "townId", "districtId",
            "cityId", "provinceId");

    /**
     * 生成并设置地址链信息并设置最小节点id
     *
     * @param addressVO
     * @author fangs
     */
    public static void reFormatAddress(AddressVO addressVO) {
        Map<String, Object> data = Convert.toMap(String.class, Object.class, addressVO);
        setAddressChain(data, addressVO);
        addressVO.setMiniAreaId(getMiniAreaId(data));
    }

    /**
     * 生成并设置地址链信息
     *
     * @param addressVO 格式化后的地址信息，地址链生成后将设置该对象的treeVOList属性
     * @author fangs
     */
    public static void setAddressChain(AddressVO addressVO) {
        Map<String, Object> data = Convert.toMap(String.class, Object.class, addressVO);
        setAddressChain(data, addressVO);
    }

    /**
     * 生成并设置地址链信息
     *
     * @param data 格式化后的地址信息，地址链生成后将设置该对象的treeVOList属性
     * @author fangs
     */
    public static void setAddressChain(Map<String, Object> data, AddressVO addressVO) {
        List<AddressTreeVO> addressTreeVOList = new ArrayList<>();
        StringBuilder fullAddressSb = new StringBuilder();
        for (String areaType : AREA_TYPE_SORT) {
            if (data.containsKey(areaType) && StringUtils.hasText((CharSequence) data.get(areaType))) {
                String idKey = areaType + "Id";
                String codeKey = areaType + "Code";
                AddressTreeVO addressTreeVO = new AddressTreeVO();
                addressTreeVO.setAreaName((String) data.get(areaType));
                addressTreeVO.setAddressId(data.get(idKey) != null ? (Long) data.get(idKey) : null);
                addressTreeVO.setAreaCode(data.get(codeKey) != null ? (String) data.get(codeKey) : null);
                addressTreeVO.setLeafNode(0);
                addressTreeVO.setLastNode(0);
                addressTreeVO.setAreaType(areaType);
                addressTreeVO.setMajorPointer(0L);
                addressTreeVOList.add(addressTreeVO);

                fullAddressSb.append(addressTreeVO.getAreaName());
            }
        }
        if (!CollectionUtils.isEmpty(addressTreeVOList)) {
            AddressTreeVO addressTreeVO = addressTreeVOList.get(addressTreeVOList.size() - 1);
            addressTreeVO.setOriginAdd(addressVO.getOriginAdd());
            addressTreeVO.setFullAddress(fullAddressSb.toString());
            addressTreeVO.setLastNode(1);
            addressTreeVO.setLeafNode(1);
            addressTreeVO.setEduAddressId(addressVO.getEduAddressId());
            addressVO.setAddressChain(addressTreeVOList);
        }

        addressVO.setMiniAreaId(getMiniAreaId(data));
    }

    /**
     * 获取父级地址路径
     *
     * @param addressVO 格式后的地址信息
     * @return 父级地址路径 例如：XX省/XX市/XX...XX社区/X幢/X单元/
     * @author fangs
     */
    public static String getParentPath(AddressVO addressVO) {
        if (CollectionUtils.isEmpty(addressVO.getAddressChain())) {
            setAddressChain(addressVO);
        }
        List<AddressTreeVO> treeList = addressVO.getAddressChain();
        StringBuilder parentPathSb = new StringBuilder();
        for (int i = 0; i < treeList.size(); i++) {
            parentPathSb.append(treeList.get(i).getAreaName()).append("/");
            if (i == treeList.size() - 2) {
                break;
            }
        }
        return parentPathSb.toString();
    }

    /**
     * 获取格式化地址中最末级的id(有id代表地址树中已保存)
     *
     * @param addressMap
     * @return 已保存的最末级的id
     * @author fangs
     */
    private static Long getMiniAreaId(Map<String, Object> addressMap) {
        long treeId = -1;
        for (String s : AREA_ID_SORT) {
            if (addressMap.containsKey(s) && addressMap.get(s) != null) {
                treeId = (long) addressMap.get(s);
                break;
            }
        }
        return treeId;
    }
    //截取第一位数字之前的字符串
    public static String extractBeforeFirstDigit(String input) {
        if(org.apache.commons.lang3.StringUtils.isBlank(input)){
            return input;
        }
        int digitIndex = -1;
        for (int i = 0; i < input.length(); i++) {
            if (Character.isDigit(input.charAt(i))) {
                digitIndex = i;
                break;
            }
        }

        if (digitIndex > 0) {
            return input.substring(0, digitIndex);
        }
        return input; // 如果没有数字，返回原字符串
    }
    //获取从第一数字开始到中文号之间的字符串
    public static String extractHao(String input) {
        //根据/分割字符串
        String[] strArray = input.split("/");
        //遍历数组
        String result="";
        int maxLength=strArray.length-1;
        for (int i = 0; i < strArray.length; i++) {
            boolean flag=true;
            //中文字号的字符串
            if (strArray[i].contains("号")||strArray[i].contains("弄")) {
                if (i<maxLength) {
                    //判断字符串中是否存在数字
                    if (strArray[i].matches(".*\\d+.*")) {
                        if (!strArray[i+1].matches(".*\\d+.*")) {
                            //下一个字符串没有数字
                            flag = false;
                        }

                    }
                }

            }
            if (flag){
                if (org.apache.commons.lang3.StringUtils.isBlank(result)){
                    result=strArray[i];
                }else {
                    result = result + "/" + strArray[i];
                }
            }
        }
        return result;


    }

    public static void main(String [] args){
        String str= AddressUtil.extractHao("浙江省/金华市/金东区/孝顺镇/孝川路/1004号/54幢");
        System.out.println(str);
    }
}
